﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：JQueryScript.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年5月24日
 */
using System.Text;

namespace ajayumi.Platform.Web
{
    /// <summary>
    /// JQueryScript.cs 基于JQuery的脚本辅助类
    /// Author   : 黄乙冬
    /// Date     : 2011-12-01
    /// </summary>
    public class JQueryScript
    {
        #region Variables

        /// <summary>
        /// 消息框的键值
        /// </summary>
        public const string MESSAGEBOXKEY = "MessageBoxKey";
        /// <summary>
        /// 对话框的键值
        /// </summary>
        public const string DIALOGKEY = "JQueryDlg";

        #endregion

        private static string GetEffect(Effects effect)
        {
            string effectStr = string.Empty;
            switch (effect)
            {
                case Effects.None:
                    break;
                case Effects.Blind:
                    effectStr = "blind";
                    break;
                case Effects.Bounce:
                    effectStr = "bounce";
                    break;
                case Effects.Clip:
                    effectStr = "clip";
                    break;
                case Effects.Drop:
                    effectStr = "drop";
                    break;
                case Effects.Explode:
                    effectStr = "explode";
                    break;
                case Effects.Fade:
                    effectStr = "fade";
                    break;
                case Effects.Fold:
                    effectStr = "fold";
                    break;
                case Effects.Highlight:
                    effectStr = "highlight";
                    break;
                case Effects.Puff:
                    effectStr = "puff";
                    break;
                case Effects.Pulsate:
                    effectStr = "pulsate";
                    break;
                case Effects.Scale:
                    effectStr = "scale";
                    break;
                case Effects.Shake:
                    effectStr = "shake";
                    break;
                case Effects.Size:
                    effectStr = "size";
                    break;
                case Effects.Slide:
                    effectStr = "slide";
                    break;
                case Effects.Transfer:
                    effectStr = "transfer";
                    break;
            }
            return effectStr;
        }

        /// <summary>
        /// 获取弹出对话框错误状态的内容样式脚本字符串。
        /// 例如：ScriptHelper.AddScript(JQueryScript.MESSAGEBOXKEY,string.Concat(Utility.GetTimeoutReference(), JQueryScript.GetErrorContentReference()));
        /// </summary>
        /// <returns></returns>
        public static string GetErrorContentReference()
        {
            return "$(\".ui-dialog-content\").addClass(\"ui-state-error ui-corner-all\");";
        }

        /// <summary>
        /// 获取弹出对话框提示状态的内容样式脚本字符串。
        /// 例如：ScriptHelper.AddScript(JQueryScript.MESSAGEBOXKEY,string.Concat(scriptStr, JQueryScript.GetInformationContentReference()));
        /// </summary>
        /// <returns></returns>
        public static string GetInformationContentReference()
        {
            return "$(\".ui-dialog-content\").addClass(\"ui-state-highlight ui-cornet-all\");";
        }


        #region 获取关闭内嵌IFrame的弹出对话框脚本字符串
        /// <summary>
        /// 获取关闭内嵌IFrame的弹出对话框脚本字符串
        /// </summary>
        /// <returns></returns>
        public static string GetIFrameDlgCloseReference()
        {
            string selector = string.Concat("#", DIALOGKEY);
            return GetIFrameDlgCloseReference(selector);
        }

        /// <summary>
        /// 获取关闭内嵌IFrame的弹出对话框脚本字符串
        /// </summary>
        /// <returns></returns>
        public static string GetIFrameDlgCloseReference(string selector)
        {
            return string.Format("window.parent.$(\"{0}\").dialog(\"close\");", selector);
        }
        #endregion

        #region 获取自动关闭对话框的脚本字符串

        /// <summary>
        /// 获取自动关闭对话框的脚本字符串
        /// </summary>
        /// <param name="title">标题（为空则不显示标题栏）</param>
        /// <param name="content">内容</param>
        /// <param name="isModel">是否为模态对话框</param>
        /// <param name="closeTime">自动关闭的时间，单位为毫秒</param>
        /// <returns></returns>
        public static string GetAutoCloseMessageBoxReference(string title,
            string content,
            bool isModel,
            int closeTime)
        {
            return GetMessageBoxReference(title, content, isModel, false, 0, 0, Effects.None, Effects.None, closeTime);
        }

        /// <summary>
        /// 获取自动关闭对话框的脚本字符串
        /// </summary>
        /// <param name="title">标题（为空则不显示标题栏）</param>
        /// <param name="content">内容</param>
        /// <param name="isModel">是否为模态对话框</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="closeTime">自动关闭的时间，单位为毫秒</param>
        /// <returns></returns>
        public static string GetAutoCloseMessageBoxReference(string title,
            string content,
            bool isModel,
            int width,
            int height,
            int closeTime)
        {
            return GetMessageBoxReference(title, content, isModel, false, width, height, Effects.None, Effects.None, closeTime);
        }

        /// <summary>
        /// 获取自动关闭对话框的脚本字符串
        /// </summary>
        /// <param name="title">标题（为空则不显示标题栏）</param>
        /// <param name="content">内容</param>
        /// <param name="isModel">是否为模态对话框</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="showEffect">显示时的动画效果，为空表示无动画</param>
        /// <param name="hideEffect">关闭时的动画效果，为空表示无动画</param>
        /// <param name="closeTime">自动关闭的时间，单位为毫秒</param>
        /// <returns></returns>
        public static string GetAutoCloseMessageBoxReference(string title,
            string content,
            bool isModel,
            int width,
            int height,
            Effects showEffect,
            Effects hideEffect,
            int closeTime)
        {
            return GetMessageBoxReference(title, content, isModel, false, width, height, showEffect, hideEffect, closeTime);
        }

        #endregion

        #region 获取消息对话框的脚本字符串
        /// <summary>
        /// 获取消息对话框的脚本字符串
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public static string GetMessageBoxReference(string title, string content)
        {
            return GetMessageBoxReference(title, content, true, true, 0, 0, Effects.None, Effects.None, 0);
        }

        /// <summary>
        /// 获取消息对话框的脚本字符串
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="isModel">是否为模态对话框</param>
        /// <param name="resizable">是否可以调整</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="showEffect">显示时的动画效果，为空表示无动画</param>
        /// <param name="hideEffect">关闭时的动画效果，为空表示无动画</param>
        /// <param name="closeTime">自动关闭的时间，单位为毫秒。0表示不关闭</param>
        /// <returns></returns>
        public static string GetMessageBoxReference(string title,
            string content,
            bool isModel,
            bool resizable,
            int width,
            int height,
            Effects showEffect,
            Effects hideEffect,
            int closeTime)
        {
            return GetMessageBoxReference(title, content, isModel, resizable, width, height, showEffect, hideEffect, closeTime, string.Empty);
        }

        /// <summary>
        /// 获取消息对话框的脚本字符串
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="isModel">是否为模态对话框</param>
        /// <param name="resizable">是否可以调整</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="showEffect">显示时的动画效果，为空表示无动画</param>
        /// <param name="hideEffect">关闭时的动画效果，为空表示无动画</param>
        /// <param name="closeTime">自动关闭的时间，单位为毫秒。0表示不关闭</param>
        /// <param name="callbackScript"></param>
        /// <returns></returns>
        public static string GetMessageBoxReference(string title,
            string content,
            bool isModel,
            bool resizable,
            int width,
            int height,
            Effects showEffect,
            Effects hideEffect,
            int closeTime,
            string callbackScript)
        {
            const string divId = "MessageBox";
            StringBuilder sbScript = new StringBuilder();
            sbScript.AppendFormat("$(\"body\").append(\"<div id='{0}'></div>\");", divId);
            sbScript.AppendLine();
            sbScript.Append("var timeId = null;");
            sbScript.AppendFormat("var dlg = $(\"#{0}\");", divId);
            sbScript.AppendFormat("dlg.html('{0}');", content);
            sbScript.Append("dlg.dialog({");
            sbScript.AppendFormat("modal: {0},", isModel ? "true" : "false");
            sbScript.AppendFormat("resizable: {0},", resizable ? "true" : "false");
            sbScript.AppendFormat("title: '{0}',", title);
            if (width > 0)
            { sbScript.AppendFormat("width: '{0}',", width); }
            if (height > 0)
            { sbScript.AppendFormat("height: '{0}',", height); }
            sbScript.AppendFormat("show: '{0}',", GetEffect(showEffect));
            sbScript.AppendFormat("hide: '{0}',", GetEffect(hideEffect));
            if (string.IsNullOrEmpty(callbackScript))
            { sbScript.Append("beforeClose: function(event,ui){ clearTimeout(timeId); }"); }
            else
            { sbScript.AppendFormat("beforeClose: function(event,ui){{ clearTimeout(timeId); {0}; }}", callbackScript); }

            sbScript.Append("});");
            sbScript.AppendLine();
            if (string.IsNullOrEmpty(title))
            {
                sbScript.Append("$(\".ui-dialog-titlebar\").hide();");
            }
            if (closeTime > 0)
            {
                sbScript.AppendFormat("timeId = setTimeout(function () {{ dlg.dialog(\"close\"); }}, {0});", closeTime);
            }

            return sbScript.ToString();
        }
        #endregion

        /// <summary>
        /// 动画效果
        /// </summary>
        public enum Effects
        {
            None,
            Blind,
            Bounce,
            Clip,
            Drop,
            Explode,
            Fade,
            Fold,
            Highlight,
            Puff,
            Pulsate,
            Scale,
            Shake,
            Size,
            Slide,
            Transfer
        }
    }
}
